﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Notewp.Model;

namespace Notewp.ViewModel
{
    public class BussinessData
    {
        //DB_NoteContext la 1 class thuoc Notedatacontext.cs
        private static string con = databaseContext.ConnectionString;// biến con read/write 
        //Get all list note
        public static IList<TblNote> _get_list_note()
        {
            try
            {
                IList<TblNote> _lstNote;
                using (var context = new databaseContext(con))
                {
                    _lstNote = context.TblNote.ToList();

                }
                return _lstNote;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        //get list by TagID
        public static IList<TblNote> _get_list_note_by_tag(int tag)
        {
            try
            {
                IList<TblNote> _lstNote;
                using (var context = new databaseContext(con))
                {
                    _lstNote = (from p in context.TblNote where p.TagID == tag select p).ToList();

                }
                return _lstNote;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        //get count by TagID
        public static int _get_count_note_by_tag(int tag)
        {
            try
            {
                int sl;
                using (var context = new databaseContext(con))
                {
                    sl = (from p in context.TblNote where p.TagID == tag select p).Count();

                }
                return sl;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        //get count by TagID
        public static int _get_count_note()
        {
            try
            {
                int sl;
                using (var context = new databaseContext(con))
                {
                    sl = (from p in context.TblNote select p).Count();

                }
                return sl;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        //search by content or by tag
        //get list by TagID
        public static IList<TblNote> _get_list_note_by_tag(string content)
        {
            try
            {
                IList<TblNote> _lstNote;
                using (var context = new databaseContext(con))
                {
                    //string input_content=content.Trim().Substring(0,1);
                    _lstNote = (from p in context.TblNote where p.NoiDung.Contains(content) select p).ToList();

                }
                return _lstNote;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
       
    }
}
